package com.fwj.base.entity;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * api返回对象
 *
 * @param <T>
 * @author ZC
 */
@Slf4j
@Data
public class R<T> {
    public static final Integer SUCCESS_CODE = 0;
    public static final Integer FAILURE_CODE = 1;

    public static final String SUCCESS_MSG = "请求成功";
    public static final String FAILURE_MSG = "请求失败";

    private Integer code;
    private String message;
    private T data;

    public static <T> R<T> ok(T data) {
        return new R(SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public static <T> R<T> ok(T data, String message) {
        return new R(SUCCESS_CODE, message, data);
    }

    public static <T> R<T> failed() {
        return new R(FAILURE_CODE, FAILURE_MSG, null);
    }

    public static <T> R<T> failed(String message) {
        return new R(FAILURE_CODE, message, null);
    }

    public static <T> R<T> failed(Exception e) {
        log.error("请求错误：", e);
        return new R(FAILURE_CODE, e.getMessage(), null);
    }

    public R(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
